package com.lanou.wuyou.one.call;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.lanou.wuyou.one.R;

/**
 * Created by dllo on 17/3/4.
 */

public class CallFragment extends Fragment implements View.OnClickListener {
    private ImageView zero, one, two, three, four, five, six, seven, eight, nine, call, delete;
    private EditText edit;
    private RelativeLayout relativeLayout;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_call, container, false);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        one = (ImageView) view.findViewById(R.id.one_iv);
        two = (ImageView) view.findViewById(R.id.two_iv);
        three = (ImageView) view.findViewById(R.id.three_iv);
        four = (ImageView) view.findViewById(R.id.four_iv);
        five = (ImageView) view.findViewById(R.id.five_iv);
        six = (ImageView) view.findViewById(R.id.six_iv);
        seven = (ImageView) view.findViewById(R.id.seven_iv);
        eight = (ImageView) view.findViewById(R.id.eight_iv);
        nine = (ImageView) view.findViewById(R.id.nine_iv);
        zero = (ImageView) view.findViewById(R.id.zero_iv);
        call = (ImageView) view.findViewById(R.id.call_iv);
        edit = (EditText) view.findViewById(R.id.number_et);
        edit.clearFocus();
        delete = (ImageView) view.findViewById(R.id.delete_iv);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        one.setOnClickListener(this);
        two.setOnClickListener(this);
        three.setOnClickListener(this);
        four.setOnClickListener(this);
        five.setOnClickListener(this);
        six.setOnClickListener(this);
        seven.setOnClickListener(this);
        eight.setOnClickListener(this);
        nine.setOnClickListener(this);
        zero.setOnClickListener(this);
        call.setOnClickListener(this);
        edit.setOnClickListener(this);
        delete.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int position = edit.getSelectionStart();
        Editable number = edit.getText();
        switch (v.getId()) {
            case R.id.one_iv:
                number.insert(position, "1");
                break;
            case R.id.two_iv:
                number.insert(position, "2");
                break;
            case R.id.three_iv:
                number.insert(position, "3");
                break;
            case R.id.four_iv:
                number.insert(position, "4");
                break;
            case R.id.five_iv:
                number.insert(position, "5");
                break;
            case R.id.six_iv:
                number.insert(position, "6");
                break;
            case R.id.seven_iv:
                number.insert(position, "7");
                break;
            case R.id.eight_iv:
                number.insert(position, "8");
                break;
            case R.id.nine_iv:
                number.insert(position, "9");
                break;
            case R.id.zero_iv:
                number.insert(position, "0");
                break;
            case R.id.delete_iv:
                int keyCode = KeyEvent.KEYCODE_DEL;
                KeyEvent keyDown = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
                KeyEvent keyUp = new KeyEvent(KeyEvent.ACTION_UP, keyCode);
                edit.onKeyDown(keyCode, keyDown);
                edit.onKeyUp(keyCode, keyUp);
                break;
            case R.id.call_iv:
                if (number.length() != 0) {
                    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + number));
                    startActivity(intent);
                } else {
                    Toast.makeText(getContext(), "内容为空", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}
